package com.xmtlxx.tms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.tms.domain.bo.CarrierBo;
import com.xmtlxx.tms.domain.vo.CarrierVo;
import com.xmtlxx.tms.service.ICarrierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 承运商Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "承运商控制器", tags = {"承运商管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/tms/carrier")
public class CarrierController extends BaseController {

    private final ICarrierService iCarrierService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<CarrierVo> initBean(){
        CarrierVo vo = new CarrierVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询承运商列表
     */
    @ApiOperation("查询承运商列表")
    @SaCheckPermission("tms:carrier:list")
    @GetMapping("/list")
    public TableDataInfo<CarrierVo> list(@Validated(QueryGroup.class) CarrierBo bo, PageQuery pageQuery) {
        return iCarrierService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询承运商列表
     */
    @ApiOperation("查询承运商列表")
    @SaCheckPermission("tms:carrier:list")
    @GetMapping("/query")
    public R<List<CarrierVo>> query(@Validated(QueryGroup.class) CarrierBo bo) {
        return R.ok(iCarrierService.queryList(bo));
    }

    /**
     * 导出承运商列表
     */
    @ApiOperation("导出承运商列表")
    @SaCheckPermission("tms:carrier:export")
    @Log(title = "承运商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated CarrierBo bo, HttpServletResponse response) {
        List<CarrierVo> list = iCarrierService.queryList(bo);
        ExcelUtil.exportExcel(list, "承运商", CarrierVo.class, response);
    }

    /**
     * 获取承运商详细信息
     */
    @ApiOperation("获取承运商详细信息")
    @SaCheckPermission("tms:carrier:query")
    @GetMapping("/{id}")
    public R<CarrierVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iCarrierService.queryById(id));
    }

    /**
     * 新增承运商
     */
    @ApiOperation("新增承运商")
    @SaCheckPermission("tms:carrier:add")
    @Log(title = "承运商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CarrierVo vo) {
        CarrierBo bo = new CarrierBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iCarrierService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改承运商
     */
    @ApiOperation("修改承运商")
    @SaCheckPermission("tms:carrier:edit")
    @Log(title = "承运商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CarrierVo vo) {
        CarrierBo bo = new CarrierBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iCarrierService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除承运商
     */
    @ApiOperation("删除承运商")
    @SaCheckPermission("tms:carrier:remove")
    @Log(title = "承运商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iCarrierService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
